{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "0506.ipynb",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "GPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/0506.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "c4E3B_wbPxdy",
        "colab_type": "text"
      },
      "source": [
        "## 2.2 数据操作"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5p7g6yIZqEyf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "!pip install mxnet d2lzh "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0yLbfAicqaGx",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from mxnet import nd "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CMmpz5QrqmvA",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "1706b76b-0374-4709-dd39-54369e0206bd"
      },
      "source": [
        "x = nd.arange(12)\n",
        "x"
      ],
      "execution_count": 3,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[ 0.  1.  2.  3.  4.  5.  6.  7.  8.  9. 10. 11.]\n",
              "<NDArray 12 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 3
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-4MAETXUqp6h",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "c345f20a-33e6-48ee-bfc3-938371efe035"
      },
      "source": [
        "x.shape"
      ],
      "execution_count": 4,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "(12,)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 4
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "opPsFSD7qr8g",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "39216557-9159-4d96-ccef-284b7d455b73"
      },
      "source": [
        "x.size "
      ],
      "execution_count": 5,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "12"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 5
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gJSbn_j0qtuB",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "149e50fe-677f-46ff-ee13-0b6df58afb97"
      },
      "source": [
        "X = x.reshape((3, 4))\n",
        "X"
      ],
      "execution_count": 6,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 6
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7_9XAIkZqxhx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 168
        },
        "outputId": "7d1a306e-dd8a-4e22-84ec-fa5d8ec10350"
      },
      "source": [
        "nd.zeros((2, 3, 4))"
      ],
      "execution_count": 7,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[[0. 0. 0. 0.]\n",
              "  [0. 0. 0. 0.]\n",
              "  [0. 0. 0. 0.]]\n",
              "\n",
              " [[0. 0. 0. 0.]\n",
              "  [0. 0. 0. 0.]\n",
              "  [0. 0. 0. 0.]]]\n",
              "<NDArray 2x3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 7
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1_QRgKgxq5Lw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "04e36808-0313-4502-b369-4c8e79154d04"
      },
      "source": [
        "nd.ones((3, 4))"
      ],
      "execution_count": 8,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[1. 1. 1. 1.]\n",
              " [1. 1. 1. 1.]\n",
              " [1. 1. 1. 1.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 8
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "I3xjbzWyq7yo",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "a9b0b120-2e70-49c1-e29b-e12185fe981c"
      },
      "source": [
        "Y = nd.array([[2, 1, 3, 4], [1, 2, 3, 4], [4, 3, 2, 1]])\n",
        "Y"
      ],
      "execution_count": 9,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[2. 1. 3. 4.]\n",
              " [1. 2. 3. 4.]\n",
              " [4. 3. 2. 1.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 9
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PvgjDoAHrFAI",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "cffa76e5-1dfc-4e17-db91-e71adddc7798"
      },
      "source": [
        "nd.random.normal(0, 1, shape=(3, 4))"
      ],
      "execution_count": 11,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 2.2122064   0.7740038   1.0434403   1.1839255 ]\n",
              " [ 1.8917114  -1.2347414  -1.771029   -0.45138445]\n",
              " [ 0.57938355 -1.856082   -1.9768796  -0.20801921]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 11
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TtwOl1uirLEn",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "e7d7cc0e-75b1-4832-fc1e-eb725b55a189"
      },
      "source": [
        "X + Y "
      ],
      "execution_count": 12,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 2.  2.  5.  7.]\n",
              " [ 5.  7.  9. 11.]\n",
              " [12. 12. 12. 12.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 12
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Ta96IJnfrSCP",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "853410d2-8d36-428a-f644-c43b88b281cc"
      },
      "source": [
        "X * Y "
      ],
      "execution_count": 13,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  6. 12.]\n",
              " [ 4. 10. 18. 28.]\n",
              " [32. 27. 20. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 13
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "VZcdYsuSrThv",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "f3bcc06e-4636-4e56-84a6-960b3afd4c3d"
      },
      "source": [
        "X / Y "
      ],
      "execution_count": 14,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.         1.         0.6666667  0.75     ]\n",
              " [ 4.         2.5        2.         1.75     ]\n",
              " [ 2.         3.         5.        11.       ]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 14
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vIFnKmZQrU2O",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "eb4be950-cc5f-4bdd-f0ae-d92eea4c0002"
      },
      "source": [
        "Y.exp()"
      ],
      "execution_count": 15,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 7.389056   2.7182817 20.085537  54.59815  ]\n",
              " [ 2.7182817  7.389056  20.085537  54.59815  ]\n",
              " [54.59815   20.085537   7.389056   2.7182817]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 15
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dmxurwlwrYW2",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "c8180b99-ed20-4897-f64a-f27aa9bad6fe"
      },
      "source": [
        "nd.dot(X, Y.T)"
      ],
      "execution_count": 16,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 19.  20.  10.]\n",
              " [ 59.  60.  50.]\n",
              " [ 99. 100.  90.]]\n",
              "<NDArray 3x3 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 16
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qA0JrQ8LrcWO",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 151
        },
        "outputId": "165b071b-8bce-4ea0-ab01-65840fff1753"
      },
      "source": [
        "nd.concat(X, Y, dim=0)"
      ],
      "execution_count": 18,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]\n",
              " [ 2.  1.  3.  4.]\n",
              " [ 1.  2.  3.  4.]\n",
              " [ 4.  3.  2.  1.]]\n",
              "<NDArray 6x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 18
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vE7h3Duprfco",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "cb41b3d6-a2fa-4781-a567-e7779f3798f4"
      },
      "source": [
        "nd.concat(X, Y, dim=1)"
      ],
      "execution_count": 19,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.  2.  1.  3.  4.]\n",
              " [ 4.  5.  6.  7.  1.  2.  3.  4.]\n",
              " [ 8.  9. 10. 11.  4.  3.  2.  1.]]\n",
              "<NDArray 3x8 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 19
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "I1lMam5Qr0Um",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "X = nd.arange(12).reshape((3, 4))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wEZVAu-lrpyN",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "6496389d-301e-4e84-d46b-ba75c6c577e1"
      },
      "source": [
        "X == Y "
      ],
      "execution_count": 23,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[0. 1. 0. 0.]\n",
              " [0. 0. 0. 0.]\n",
              " [0. 0. 0. 0.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 23
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LqyT0F0ersU1",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "dca10ea4-e6a4-4ba7-a767-03a3dcc5258c"
      },
      "source": [
        "X"
      ],
      "execution_count": 24,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 24
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "u50EKyWJrwLF",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "465b596d-dd01-475c-d5a8-7be4669aae9b"
      },
      "source": [
        "X.sum()"
      ],
      "execution_count": 25,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[66.]\n",
              "<NDArray 1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 25
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2TBNZeshr83Z",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "79f5668c-1d70-4e66-a9e6-4b227ec43b89"
      },
      "source": [
        "X.norm().asscalar()"
      ],
      "execution_count": 26,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "22.494442"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 26
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Gb0KB35JsDHl",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "0b670dfa-7fa0-46c7-af44-2c790e387120"
      },
      "source": [
        "A = nd.arange(3).reshape((3, 1))\n",
        "B = nd.arange(2).reshape((1, 2))\n",
        "A"
      ],
      "execution_count": 27,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[0.]\n",
              " [1.]\n",
              " [2.]]\n",
              "<NDArray 3x1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 27
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XI1jX7p3sNBs",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "9e5f5e35-4f81-4da0-9bf0-039642a5d101"
      },
      "source": [
        "B"
      ],
      "execution_count": 28,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[0. 1.]]\n",
              "<NDArray 1x2 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 28
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CJ7pM9K0sN_W",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "e42c24b7-98cd-490f-cc70-d011eb8ecbfd"
      },
      "source": [
        "A + B"
      ],
      "execution_count": 29,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[0. 1.]\n",
              " [1. 2.]\n",
              " [2. 3.]]\n",
              "<NDArray 3x2 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 29
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZwdOqm_5sRJz",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "0e179182-d5cc-42f0-eec1-0ab7e3711516"
      },
      "source": [
        "X[1:3]"
      ],
      "execution_count": 30,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 2x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 30
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vsLFgPSJsUs9",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "8ffeb00e-4862-48d3-b48b-33b80c310651"
      },
      "source": [
        "X[1, 2]\n",
        "X"
      ],
      "execution_count": 31,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 31
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Mb_lUjLssZBk",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "a8603c30-88a6-4db0-f2d6-d338b052bf05"
      },
      "source": [
        "X[1:2, :] = 12 \n",
        "X "
      ],
      "execution_count": 32,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [12. 12. 12. 12.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 32
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Qq1u7vrisdo-",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "ee80822c-4ac0-4838-ba85-508896ad11ec"
      },
      "source": [
        "before = id(Y)\n",
        "Y = Y + X \n",
        "id(Y) == before "
      ],
      "execution_count": 33,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 33
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "17yKBoVwsmRs",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "cc2d44e9-7e47-4e38-85f6-d94b607e035f"
      },
      "source": [
        "Z = Y.zeros_like()\n",
        "before = id(Z)\n",
        "Z[:] = X + Y \n",
        "id(Z) == before "
      ],
      "execution_count": 36,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 36
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WoVwqbN-svok",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "d39f4ab0-ee3a-4cd3-beee-ca53dd129d31"
      },
      "source": [
        "before  = id(X)\n",
        "X += Y \n",
        "id(X) == before "
      ],
      "execution_count": 37,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 37
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "g2qvr3zys-cL",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 84
        },
        "outputId": "4a89db43-6283-430d-92c7-80512f436fa2"
      },
      "source": [
        "import numpy as np \n",
        "\n",
        "P = np.ones((2, 3))\n",
        "D = nd.array(P)\n",
        "D"
      ],
      "execution_count": 39,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[1. 1. 1.]\n",
              " [1. 1. 1.]]\n",
              "<NDArray 2x3 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 39
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "m9r3fhAetLZ7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "66632c4b-6f2f-43b3-a044-7b4c7b0d0527"
      },
      "source": [
        "D.asnumpy()"
      ],
      "execution_count": 40,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[1., 1., 1.],\n",
              "       [1., 1., 1.]], dtype=float32)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 40
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "lW4SEo0atTPj",
        "colab_type": "text"
      },
      "source": [
        "## 2.3 自动求梯度"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "UAyrIUhMtaSS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from mxnet import autograd, nd "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lWz-2YAGtdtp",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        },
        "outputId": "ff2f8cd8-41ff-4c2e-c6da-3c9c51d25dc4"
      },
      "source": [
        "x = nd.arange(4).reshape((4, 1))\n",
        "x "
      ],
      "execution_count": 42,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[0.]\n",
              " [1.]\n",
              " [2.]\n",
              " [3.]]\n",
              "<NDArray 4x1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 42
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WNmow64Ptji4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x.attach_grad()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2dPwgRxEtmOq",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "with autograd.record():\n",
        "    y = 2 * nd.dot(x.T, x)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8D5KCWb0tudn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y.backward()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4uUnoH96twEM",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 118
        },
        "outputId": "51a930fb-c3ce-42cb-fe9e-f92185841255"
      },
      "source": [
        "assert (x.grad - 4 * x).norm().asscalar() == 0 \n",
        "x.grad"
      ],
      "execution_count": 46,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.]\n",
              " [ 4.]\n",
              " [ 8.]\n",
              " [12.]]\n",
              "<NDArray 4x1 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 46
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3o5e-Eilt6cJ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "9f190376-215e-43bc-d698-07cf71dcf8f9"
      },
      "source": [
        "print(autograd.is_training())\n",
        "with autograd.record():\n",
        "    print(autograd.is_training())"
      ],
      "execution_count": 47,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "False\n",
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "F_YxpqMJuGhp",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def f(a):\n",
        "    b = a * 2 \n",
        "    print('b1:', b)\n",
        "    n = 0 \n",
        "    while b.norm().asscalar() < 1000:\n",
        "        n += 1 \n",
        "        b = b * 2\n",
        "    print('n:', n) \n",
        "    print('b2:', b)\n",
        "    if b.sum().asscalar() > 0:\n",
        "        c = b \n",
        "        print('c1:', c)\n",
        "    else:\n",
        "        c = 100 * b \n",
        "        print('c2:', c)\n",
        "    return c "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "e4pzOxyIunTO",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 370
        },
        "outputId": "5ea9cbe5-b2ca-4aa1-d609-5f29c598e694"
      },
      "source": [
        "a = nd.random.normal(shape=(3, 4))\n",
        "print(a)\n",
        "a.attach_grad()\n",
        "with autograd.record():\n",
        "    c = f(a)\n",
        "c.backward()"
      ],
      "execution_count": 87,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "\n",
            "[[-1.88909     0.65479124 -0.45481315  0.32510808]\n",
            " [-1.3002341   0.3679345   1.4534262   0.24154152]\n",
            " [ 0.47898006  0.96885103 -1.0218245  -0.06812762]]\n",
            "<NDArray 3x4 @cpu(0)>\n",
            "b1: \n",
            "[[-3.77818     1.3095825  -0.9096263   0.65021616]\n",
            " [-2.6004682   0.735869    2.9068525   0.48308304]\n",
            " [ 0.9579601   1.9377021  -2.043649   -0.13625523]]\n",
            "<NDArray 3x4 @cpu(0)>\n",
            "n: 8\n",
            "b2: \n",
            "[[-967.21405  335.2531  -232.86433  166.45534]\n",
            " [-665.71985  188.38246  744.15424  123.66926]\n",
            " [ 245.2378   496.05173 -523.17413  -34.88134]]\n",
            "<NDArray 3x4 @cpu(0)>\n",
            "c2: \n",
            "[[-96721.41   33525.312 -23286.434  16645.533]\n",
            " [-66571.984  18838.246  74415.42   12366.926]\n",
            " [ 24523.78   49605.17  -52317.414  -3488.134]]\n",
            "<NDArray 3x4 @cpu(0)>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9qpCH9KqvSKb",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "1c5c54ef-a438-4161-cb7b-4bf88f87b012"
      },
      "source": [
        "a.grad == c / a "
      ],
      "execution_count": 88,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[1. 1. 1. 1.]\n",
              " [1. 1. 1. 1.]\n",
              " [1. 1. 1. 1.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 88
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RmihrRcmw96e",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "a480b19f-ecfb-4bbc-9364-e40034f0062f"
      },
      "source": [
        "a.grad "
      ],
      "execution_count": 89,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[51200. 51200. 51200. 51200.]\n",
              " [51200. 51200. 51200. 51200.]\n",
              " [51200. 51200. 51200. 51200.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 89
        }
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "wQ8BPmEDxFHJ",
        "colab_type": "text"
      },
      "source": [
        "# PyTorch "
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "GS-ozszbzqei",
        "colab_type": "text"
      },
      "source": [
        "## 2.2 数据操作"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JLk80r6Gzvry",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EWZmgkF2zx-G",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "a9320d98-a15e-4f7c-cedf-25ba3eae02c9"
      },
      "source": [
        "x = torch.empty(5, 3)\n",
        "print(x)"
      ],
      "execution_count": 112,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[2.2906e-34, 0.0000e+00, 4.4842e-44],\n",
            "        [0.0000e+00,        nan, 5.1208e-01],\n",
            "        [1.3554e-05, 3.4152e-06, 1.3090e+22],\n",
            "        [2.1876e-04, 8.2290e-10, 4.1729e+21],\n",
            "        [4.2007e-05, 5.3772e+22, 1.3368e+00]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lR0PWxtc0rUE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "23ab04f3-bafc-4c39-a36e-30f76657d48c"
      },
      "source": [
        "x"
      ],
      "execution_count": 113,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[2.2906e-34, 0.0000e+00, 4.4842e-44],\n",
              "        [0.0000e+00,        nan, 5.1208e-01],\n",
              "        [1.3554e-05, 3.4152e-06, 1.3090e+22],\n",
              "        [2.1876e-04, 8.2290e-10, 4.1729e+21],\n",
              "        [4.2007e-05, 5.3772e+22, 1.3368e+00]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 113
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HrgXJemV0yCh",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "aba27eb6-95ec-4ab6-c8f3-11792aab88d0"
      },
      "source": [
        "x = torch.rand(5, 3)\n",
        "x"
      ],
      "execution_count": 114,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0.9742, 0.3791, 0.2096],\n",
              "        [0.6549, 0.2182, 0.4989],\n",
              "        [0.2401, 0.9136, 0.3931],\n",
              "        [0.6601, 0.3845, 0.5110],\n",
              "        [0.5142, 0.8150, 0.4168]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 114
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "q8DrS1Bg02pP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# torch.rand??"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MInxSI7B059A",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "f43bb6c1-3460-4812-8709-5e0f29f897b4"
      },
      "source": [
        "x = torch.zeros(5, 3, dtype=torch.long)\n",
        "x"
      ],
      "execution_count": 116,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0, 0, 0],\n",
              "        [0, 0, 0],\n",
              "        [0, 0, 0],\n",
              "        [0, 0, 0],\n",
              "        [0, 0, 0]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 116
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TIhvhLgN2Mdv",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "14a52545-539b-4ffb-de1b-5ce381702943"
      },
      "source": [
        "x = torch.tensor([5.5, 3])\n",
        "x"
      ],
      "execution_count": 117,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([5.5000, 3.0000])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 117
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Rjj1r7mb2Svd",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "e3616fe3-4708-415a-d807-9475d4443f08"
      },
      "source": [
        "x = x.new_ones(5, 3, dtype=torch.float64)\n",
        "x"
      ],
      "execution_count": 118,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1.],\n",
              "        [1., 1., 1.],\n",
              "        [1., 1., 1.],\n",
              "        [1., 1., 1.],\n",
              "        [1., 1., 1.]], dtype=torch.float64)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 118
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AgFdDkzC2fvu",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "c6b5953b-071c-49d8-d85f-90cb371c31f3"
      },
      "source": [
        "x = torch.randn_like(x, dtype=torch.float)\n",
        "x "
      ],
      "execution_count": 119,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.3382,  1.0064,  1.3071],\n",
              "        [-1.1685, -1.0985, -0.7131],\n",
              "        [-1.5762,  1.0393, -0.2857],\n",
              "        [ 0.2230, -0.4475, -0.0058],\n",
              "        [ 0.8169,  0.2681, -0.8816]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 119
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DPPxS9YL2nYE",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# torch.randn_like??"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8HshJXW72v6i",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# for item in dir(torch.nn):\n",
        "#     print(item)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uzYUPOyZ7Oes",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "f3ef4fe8-9e58-4b94-8685-f66b070d75c8"
      },
      "source": [
        "x.size()"
      ],
      "execution_count": 122,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "torch.Size([5, 3])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 122
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oYl1G0-OUGuw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "1d00eb03-5fc8-42d2-fa31-e149c46e981c"
      },
      "source": [
        "print(x.size())"
      ],
      "execution_count": 123,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "torch.Size([5, 3])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "63Z6wfd9UKXN",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "17a0b334-1b00-451c-e5a9-986d894952f9"
      },
      "source": [
        "print(x.size)"
      ],
      "execution_count": 124,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "<built-in method size of Tensor object at 0x7fd36084b1b0>\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vkK48nsKUMkV",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a4af26c5-62e0-481e-cef4-3305a0d1ca9e"
      },
      "source": [
        "print(x.shape)"
      ],
      "execution_count": 125,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "torch.Size([5, 3])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Y83sV2pSUUfl",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "3fdb6e30-6dc8-4a07-8080-544589fc78b9"
      },
      "source": [
        "print(x.shape)"
      ],
      "execution_count": 127,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "torch.Size([5, 3])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "89L8N9E3UYT1",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "d0bc7130-57b2-4c7b-d536-c06e70136841"
      },
      "source": [
        "x"
      ],
      "execution_count": 128,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[-0.3382,  1.0064,  1.3071],\n",
              "        [-1.1685, -1.0985, -0.7131],\n",
              "        [-1.5762,  1.0393, -0.2857],\n",
              "        [ 0.2230, -0.4475, -0.0058],\n",
              "        [ 0.8169,  0.2681, -0.8816]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 128
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Tlb5GpcyUpbL",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "641d3681-5ca3-417b-8528-af40698ff823"
      },
      "source": [
        "y = torch.rand(5, 3)\n",
        "x + y "
      ],
      "execution_count": 129,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.4636,  1.4516,  1.6162],\n",
              "        [-0.2766, -0.4191, -0.0724],\n",
              "        [-1.1186,  1.2172, -0.0934],\n",
              "        [ 1.1718,  0.4459, -0.0020],\n",
              "        [ 1.3284,  0.6314, -0.0286]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 129
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9DG8MEj8UvRx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "a08ee76f-9d93-4488-aa2f-962e659ff572"
      },
      "source": [
        "torch.add(x, y)"
      ],
      "execution_count": 130,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.4636,  1.4516,  1.6162],\n",
              "        [-0.2766, -0.4191, -0.0724],\n",
              "        [-1.1186,  1.2172, -0.0934],\n",
              "        [ 1.1718,  0.4459, -0.0020],\n",
              "        [ 1.3284,  0.6314, -0.0286]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 130
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PWzv8OzpUysJ",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "740f1581-78e6-48f9-85a6-c59ede2b91a7"
      },
      "source": [
        "result = torch.empty(5, 3)\n",
        "torch.add(x, y, out=result)\n",
        "result"
      ],
      "execution_count": 131,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.4636,  1.4516,  1.6162],\n",
              "        [-0.2766, -0.4191, -0.0724],\n",
              "        [-1.1186,  1.2172, -0.0934],\n",
              "        [ 1.1718,  0.4459, -0.0020],\n",
              "        [ 1.3284,  0.6314, -0.0286]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 131
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "bJ_cxyCmU-ky",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# torch.empty??"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nyKNXLbcVDO7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "064d62ea-aee3-4d13-ff85-1b56d310c230"
      },
      "source": [
        "torch.empty(2, 3)"
      ],
      "execution_count": 135,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[2.2908e-34, 0.0000e+00, 4.4842e-44],\n",
              "        [0.0000e+00,        nan, 6.4460e-44]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 135
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WfdBc_EwVu-y",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "150167c1-eb57-4512-ff4e-4aeb2a4e400e"
      },
      "source": [
        "y.add_(x)\n",
        "y"
      ],
      "execution_count": 136,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.4636,  1.4516,  1.6162],\n",
              "        [-0.2766, -0.4191, -0.0724],\n",
              "        [-1.1186,  1.2172, -0.0934],\n",
              "        [ 1.1718,  0.4459, -0.0020],\n",
              "        [ 1.3284,  0.6314, -0.0286]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 136
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mzFaiqdnV80Z",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "45f94830-94ee-4c06-f5d5-a2cedd66dc55"
      },
      "source": [
        "y = x[0, :]\n",
        "print(y)\n",
        "y += 1 \n",
        "print(y)\n",
        "print(x[0, :])"
      ],
      "execution_count": 137,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([-0.3382,  1.0064,  1.3071])\n",
            "tensor([0.6618, 2.0064, 2.3071])\n",
            "tensor([0.6618, 2.0064, 2.3071])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aPQBx4gnWSPO",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "1ff1859b-9ddc-47d6-d55a-f24b7e553f0a"
      },
      "source": [
        "x "
      ],
      "execution_count": 138,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0.6618,  2.0064,  2.3071],\n",
              "        [-1.1685, -1.0985, -0.7131],\n",
              "        [-1.5762,  1.0393, -0.2857],\n",
              "        [ 0.2230, -0.4475, -0.0058],\n",
              "        [ 0.8169,  0.2681, -0.8816]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 138
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "S5CyD8tbWuxe",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "79244e81-9c50-4b98-fcba-34eab82e8d41"
      },
      "source": [
        "y = x.view(15)\n",
        "print(y)"
      ],
      "execution_count": 139,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([ 0.6618,  2.0064,  2.3071, -1.1685, -1.0985, -0.7131, -1.5762,  1.0393,\n",
            "        -0.2857,  0.2230, -0.4475, -0.0058,  0.8169,  0.2681, -0.8816])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YeaR7KQmWzxm",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "f432270c-e203-4d67-92d0-6ad78fd0361b"
      },
      "source": [
        "print(y.size())"
      ],
      "execution_count": 140,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "torch.Size([15])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uqPG7pclW6B9",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "e948f5e0-6cc5-48be-a16c-8954a48209a2"
      },
      "source": [
        "z = x.view(-1, 5)\n",
        "print(z)"
      ],
      "execution_count": 141,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[ 0.6618,  2.0064,  2.3071, -1.1685, -1.0985],\n",
            "        [-0.7131, -1.5762,  1.0393, -0.2857,  0.2230],\n",
            "        [-0.4475, -0.0058,  0.8169,  0.2681, -0.8816]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-LbiceiFXAsX",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "c956c04c-3bfb-4187-e969-3453ca165ee0"
      },
      "source": [
        "x += 1 \n",
        "print(x)"
      ],
      "execution_count": 142,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[ 1.6618,  3.0064,  3.3071],\n",
            "        [-0.1685, -0.0985,  0.2869],\n",
            "        [-0.5762,  2.0393,  0.7143],\n",
            "        [ 1.2230,  0.5525,  0.9942],\n",
            "        [ 1.8169,  1.2681,  0.1184]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JlApAA-9XNaE",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "5eb85500-f9ce-47e1-f7a3-35c55650fff8"
      },
      "source": [
        "print(y)"
      ],
      "execution_count": 143,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([ 1.6618,  3.0064,  3.3071, -0.1685, -0.0985,  0.2869, -0.5762,  2.0393,\n",
            "         0.7143,  1.2230,  0.5525,  0.9942,  1.8169,  1.2681,  0.1184])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JZB55NWMXQ69",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# x.view??"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ECs_fpYgYoYO",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "16067aa1-9374-4eb4-a045-9c2739bb2774"
      },
      "source": [
        "X = torch.arange(12).view(3, 4)\n",
        "X"
      ],
      "execution_count": 148,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[ 0,  1,  2,  3],\n",
              "        [ 4,  5,  6,  7],\n",
              "        [ 8,  9, 10, 11]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 148
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zLUlQFyEZCYw",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "c9050935-d605-46b8-b8d6-58a848c48a53"
      },
      "source": [
        "x_cp = x.clone().view(15)\n",
        "x -= 1 \n",
        "print(x)"
      ],
      "execution_count": 149,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([[ 0.6618,  2.0064,  2.3071],\n",
            "        [-1.1685, -1.0985, -0.7131],\n",
            "        [-1.5762,  1.0393, -0.2857],\n",
            "        [ 0.2230, -0.4475, -0.0058],\n",
            "        [ 0.8169,  0.2681, -0.8816]])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "QGpRWUQyZTyg",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "c1de6f55-17ff-49ed-d041-0bbd4ae05100"
      },
      "source": [
        "print(x_cp)"
      ],
      "execution_count": 150,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([ 1.6618,  3.0064,  3.3071, -0.1685, -0.0985,  0.2869, -0.5762,  2.0393,\n",
            "         0.7143,  1.2230,  0.5525,  0.9942,  1.8169,  1.2681,  0.1184])\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WuroDghNZWYP",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "b03eaae6-9495-4524-858f-af009143699a"
      },
      "source": [
        "x = torch.randn(1)\n",
        "x"
      ],
      "execution_count": 151,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([-0.3648])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 151
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "G-HKgjwxZnUP",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "3f33777a-8c28-4a06-d60f-a4ad44fa90d1"
      },
      "source": [
        "x.item()"
      ],
      "execution_count": 152,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "-0.36481809616088867"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 152
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cdsD0skSZqmH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# x.item??"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "dUHPI1plcBWR",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "my_x = nd.arange(12).reshape((3, 4))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TJKQELA6eeOq",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 101
        },
        "outputId": "8e65c932-a2e1-4ec3-9207-d9b43be97434"
      },
      "source": [
        "my_x"
      ],
      "execution_count": 157,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "\n",
              "[[ 0.  1.  2.  3.]\n",
              " [ 4.  5.  6.  7.]\n",
              " [ 8.  9. 10. 11.]]\n",
              "<NDArray 3x4 @cpu(0)>"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 157
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ymoP740jehYz",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "74a60cd0-59bc-4f82-9ebf-bf24e46fd627"
      },
      "source": [
        "my_x.sum().asscalar()"
      ],
      "execution_count": 160,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "66.0"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 160
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "er6hfdMeexEb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "A = torch.arange(3).view(3, 1)\n",
        "B = torch.arange(2).view(1, 2)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "o8WiYwayhNUj",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "c340887b-818c-4143-bb77-15fdff79ccf6"
      },
      "source": [
        "A"
      ],
      "execution_count": 162,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0],\n",
              "        [1],\n",
              "        [2]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 162
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "xuUZA5eChOPl",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "1a083592-d9d0-4628-87fb-375b0612f70c"
      },
      "source": [
        "B"
      ],
      "execution_count": 163,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0, 1]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 163
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Cn3RyKWIhQND",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 67
        },
        "outputId": "0b7d956f-45e3-438d-8a7d-7076890c73bf"
      },
      "source": [
        "A + B"
      ],
      "execution_count": 164,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[0, 1],\n",
              "        [1, 2],\n",
              "        [2, 3]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 164
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "PuAYIVUdhTL9",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "X = torch.tensor([1, 2])\n",
        "Y = torch.tensor([3, 4])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "XACa4x4whrkD",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "d79a29a3-ab75-4358-d1c3-f00a1c5e1491"
      },
      "source": [
        "before = id(Y)\n",
        "Y = Y + X \n",
        "id(Y) == before"
      ],
      "execution_count": 167,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "False"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 167
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DhWtI8umiImR",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for item in dir(Y):\n",
        "    print(item)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yIZfBtrdig4t",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a3847369-5f47-480e-badb-3975abdb8d0b"
      },
      "source": [
        "before = id(Y)\n",
        "Y[:] = Y + X \n",
        "id(Y) == before"
      ],
      "execution_count": 171,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 171
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Bbk1QeEui5JK",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "a89f0dd9-e918-4dc1-8480-0cf807470d83"
      },
      "source": [
        "x = torch.tensor([1, 2])\n",
        "y = torch.tensor([3, 4])\n",
        "before = id(y)\n",
        "torch.add(x, y, out=y)\n",
        "id(y) == before"
      ],
      "execution_count": 174,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "True"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 174
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NsfzhV-PjUGS",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "5f177bfb-82ed-4b01-cd60-d699cecb7d10"
      },
      "source": [
        "print(torch.cuda.is_available())"
      ],
      "execution_count": 176,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "True\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "9vI_JYXgjyv7",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 34
        },
        "outputId": "2451ea8e-5551-4377-ce40-68799b670adb"
      },
      "source": [
        "if torch.cuda.is_available():\n",
        "    device = torch.device('cuda')\n",
        "    y = torch.ones_like(x, device=device)\n",
        "    x = x.to(device)\n",
        "    z = x + y \n",
        "    print(z)"
      ],
      "execution_count": 178,
      "outputs": [
        {
          "output_type": "stream",
          "text": [
            "tensor([2, 3], device='cuda:0')\n"
          ],
          "name": "stdout"
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YEetmL7Bk3VI",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "8401ae44-7f2e-4b7c-9a4c-a42d7a1539c3"
      },
      "source": [
        "import numpy as np \n",
        "\n",
        "P = np.ones((2, 3))\n",
        "# T = torch.tensor(P)\n",
        "T = torch.from_numpy(P)\n",
        "T"
      ],
      "execution_count": 180,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "tensor([[1., 1., 1.],\n",
              "        [1., 1., 1.]], dtype=torch.float64)"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 180
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5wwqmJAflHUx",
        "colab_type": "code",
        "colab": {
          "base_uri": "https://localhost:8080/",
          "height": 50
        },
        "outputId": "85082d9a-c309-4af8-adb0-f860c3e922e2"
      },
      "source": [
        "T.numpy()"
      ],
      "execution_count": 181,
      "outputs": [
        {
          "output_type": "execute_result",
          "data": {
            "text/plain": [
              "array([[1., 1., 1.],\n",
              "       [1., 1., 1.]])"
            ]
          },
          "metadata": {
            "tags": []
          },
          "execution_count": 181
        }
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qy9hPW9Nlmki",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}